package com.harmontronics.hanma.paas.paas.service;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.MosResponse;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.I18nKeyEnum;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.HmOrg;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.SsoOrgResult;
import com.harmontronics.hanma.paas.paas.domain.organization.service.IHmOrgService;
import com.harmontronics.hanma.paas.paas.domain.organization.service.MosService;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.utils.MyBeanUtils;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service("hmOrgMosAppService")
@ConditionalOnProperty(prefix = "mos.oss",name="enabled",havingValue = "true")
public class HmOrgMosAppService implements IHmOrgService {

    @Autowired
    private MosService mosService;

    @Override
    public HmOrg findById(Long id) {
        MosResponse<SsoOrgResult> response = mosService.getOrgById(id);
        if(response.isOk()){
            SsoOrgResult orgResult = response.getData();
            return BeanUtil.toBean(orgResult,HmOrg.class);
        }
        throw new ErrorKeyException(I18nKeyEnum.SYSTEM_ERROR);
    }

    @Override
    public List<HmOrg> getAll() {
        MosResponse<List<SsoOrgResult>> response =  mosService.getAllOrg();
        if(response.isOk()){
            List<SsoOrgResult> orgResultList = response.getData();
            return MyBeanUtils.batchTransform(HmOrg.class,orgResultList);
        }
        throw new ErrorKeyException(I18nKeyEnum.SYSTEM_ERROR);
    }


    @Override
    public List<Long> selectChildrenIdWithId(Long orgId, boolean includeDeleted) {
        MosResponse<List<Long>> response = mosService.getChildrenIds(orgId);
        if(response.isOk()){
            return response.getData();
        }
        return null;
    }

    @Override
    public List<Long> selectChildrenIdWithId(Long id) {
        return selectChildrenIdWithId(id,false);
    }

    @Override
    public List<Long> selectOrgParentIdWithUserId(Long userId) {
        List<Long> parentOrgIds = mosService.getParentOrgIds(userId);
        return parentOrgIds;
    }

    @Override
    public List<Long> userBelongTo(List<Long> orgIds, Long userId) {
        String tmpOrgIds = orgIds.stream().map(id -> id.toString()).collect(Collectors.joining(","));
        MosResponse<List<Long>> response = mosService.mockUserBelongTo(tmpOrgIds,userId);
        if(response.isOk()){
            return response.getData();
        }
        return null;
    }

    @Override
    public HmOrg getRootOrg() {
        SsoOrgResult orgResult = mosService.getRootOrg();
        return BeanUtil.toBean(orgResult,HmOrg.class);
    }


    @Override
    public void save(HmOrg hmOrg) {

    }

    @Override
    public void update(HmOrg hmOrg) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void up(Long id) {

    }

    @Override
    public void down(Long id) {

    }

    @Override
    public void move(Long id, String move) {

    }

    @Override
    public void validateField(Long id, String value, Long parentId) {

    }
}
